package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.DownloadsManager;
import com.informationsystem.service.TagManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Downloads;
import com.informationsystem.model.Systemuser;
import com.informationsystem.model.Tag;
import com.informationsystem.webapp.action.BaseAction;

import java.util.List;

public class TagAction extends BaseAction implements Preparable {
    private TagManager tagManager;
    private DownloadsManager downloadsManager;
    private List tags;
    private Tag tag;
    private Long id;
    private String query;
    private Downloads downloads;

    public void setTagManager(TagManager tagManager) {
        this.tagManager = tagManager;
    }

    public List getTags() {
        return tags;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String tagId = getRequest().getParameter("tag.id");
            if (tagId != null && !tagId.equals("")) {
                tag = tagManager.get(new Long(tagId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            tags = tagManager.search(query, Tag.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            tags = tagManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public String delete() {
    	tag = tagManager.get(tag.getId());
    	for(Downloads d: tag.getDownloadses()){
    		d.getTags().remove(tag);
    		downloadsManager.save(d);
    	}
        tagManager.remove(tag.getId());
        saveMessage(getText("tag.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            tag = tagManager.get(id);
        } else {
            tag = new Tag();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (tag.getId() == null);

        tagManager.save(tag);

        String key = (isNew) ? "tag.added" : "tag.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public DownloadsManager getDownloadsManager() {
		return downloadsManager;
	}

	public void setDownloadsManager(DownloadsManager downloadsManager) {
		this.downloadsManager = downloadsManager;
	}

	public Downloads getDownloads() {
		return downloads;
	}

	public void setDownloads(Downloads downloads) {
		this.downloads = downloads;
	}
	public List getDownloadsList(){
		return downloadsManager.getAll();
	}
	public String deleteTagDownloads(){
		boolean isNew = (tag.getId() == null);
	    if (!isNew) {
	 	    downloads = downloadsManager.get(downloads.getId());
	  	   downloads.getTags().remove(tagManager.get(tag.getId()));
	     } else {
	         return INPUT;
	     }
	   downloadsManager.save(downloads);
	    return SUCCESS;
	}
	
    public String addTagDownloads() throws Exception{
		boolean isNew = (tag.getId() == null);
	    if (!isNew) {
	 	    downloads = downloadsManager.get(downloads.getId());
	  	    downloads.getTags().add(tagManager.get(tag.getId()));
	     } else {
	        return INPUT;
	     }
	     downloadsManager.save(downloads);
	     return SUCCESS;
     }
}